import java.util.Objects;
import java.util.Scanner;

public class ProductArray {
    public supermarket intArray[];
    public int size;

    public ProductArray(int length) {
        this.intArray = new supermarket[length];
    }


    public void printArray() {
        System.out.println("商品信息如下：");
        for (int i = 0; i < size; i++) {
            System.out.println(intArray[i] + " ");
        }
        System.out.println();
    }

    //商品的增加
    public void add() {
        Scanner sc = new Scanner(System.in);

        System.out.print("请输入商品名称：");
        String name = sc.nextLine();

        System.out.print("请输入商品价格：");
        float price;
        price = sc.nextFloat();

        System.out.print("请输入商品的编号：");
        String number = sc.next();

        System.out.print("请输入商品的入库时间(如2024-12-1)：");
        String time = sc.next();


        supermarket product = new supermarket(name, price, number, time);

        if (size == intArray.length) {
            System.out.println("位置已满，无法再添加商品");
        } else {
            intArray[size] = product;
            size++;
        }

        System.out.println("商品添加成功！\n");
    }

    //商品的显示
    public supermarket findproduct() {
        if (intArray == null || size <= 0) {
            System.out.println("没有商品信息！");
            return null;  // 增加对数组和大小的有效性检查
        }
        for (int i = 0; i < size; i++) {
            System.out.println("商品的信息为" + intArray[i]);
        }
        return intArray[0];
    }


    //商品的查找（根据商品名查找）
    public supermarket update() {
        Scanner number = new Scanner(System.in);
        System.out.println("请输入商品编号：");
        String number1 = number.nextLine();
        for (int i = 0; i < size; i++) {
            if (Objects.equals(intArray[i].number, number1)) {
                System.out.println("找到该信息为\t" + intArray[i]);
                return intArray[i];
            }
        }
        System.out.println("未找到该信息");
        return null;

    }

    //商品的修改
    public supermarket edit() {
        Scanner number= new Scanner(System.in);
        System.out.println("请输入你要修改的商品编号：");
        String number1= number.nextLine();
        for (int i = 0; i < size; i++) {
            if (Objects.equals(intArray[i].number, number1)) {
                Scanner contect = new Scanner(System.in);
                System.out.println("请输入你要修改的内容：" +
                        "[1]商品名称 [2]商品价格 [3]商品编号 [4]入库时间 [0]返回菜单");
                int a = contect.nextInt();
                switch (a) {
                    case 1:
                        Scanner name3 = new Scanner(System.in);
                        System.out.println("请输入新的商品名称：");
                        String name4 = name3.nextLine();
                        for (int j = 0; j < size; j++) {
                            intArray[j].name = name4;
                            System.out.println("商品名称替换成功！");
                            return intArray[j];
                        }
                        break;

                    case 2:
                        Scanner name5 = new Scanner(System.in);
                        System.out.println("请输入新的商品价格：");
                        float name6 = name5.nextInt();
                        for (int j = 0; j < size; j++) {
                            intArray[j].price = name6;
                            System.out.println("商品价格替换成功！");
                            return intArray[j];
                        }
                        break;

                    case 3:
                        Scanner name7 = new Scanner(System.in);
                        System.out.println("请输入新的商品编号：");
                        String name8 = name7.nextLine();
                        for (int j = 0; j < size; j++) {
                            intArray[j].number = name8;
                            System.out.println("商品编号替换成功！");
                            return intArray[j];
                        }
                        break;
                    case 4:
                        Scanner name9 = new Scanner(System.in);
                        System.out.println("请输入新的入库时间：");
                        String name10 = name9.nextLine();
                        for (int j = 0; j < size; j++) {
                            intArray[j].time = name10;
                            System.out.println("入库时间替换成功！");
                            return intArray[j];
                        }
                        break;
                    case 0:
                        break;
                    default:
                        System.out.println("输入错误，请重新输入!");
                        break;
                }
                return null;
            } else
              System.out.println("未找到该信息！");
        }
        return null;
    }

    //商品的删除
    public supermarket delete(){
        Scanner number = new Scanner(System.in);
        System.out.println("请输入你要删除的商品编号：");
        String number1=number.nextLine();
        for (int i=0;i<size;i++){
            if(Objects.equals(intArray[i].number, number1)){
                for (int j=i;j<size-1;j++){
                    intArray[j]=intArray[j+1];
                }
                intArray[size-1]=null;
                size--;
                System.out.println("商品删除成功！");
                return intArray[i];
            }
        }
        System.out.println("未找到该商品，无法删除！");
        return null;
    }
}
